package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Shopc;
import com.etc.entity.User;
import com.etc.util.AjaxUploadResponse;
import com.etc.util.PageData;
import com.etc.zmall.service.ShopCartService;
import com.etc.zmall.service.impl.ShopCartServiceImpl;
import com.google.gson.Gson;

/**
 * Servlet implementation class ShopCarServlet
 */
@WebServlet("/scs.do")
public class ShopCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");

		//
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("showMyCar".equals(op)) {
			showMyCar(request, response);
		} else if ("addcart".equals(op)) {
			// 添加商品到购物车
			addCart(request, response);
		}

	}

	private void addCart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置mime类型
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		//判断用户是否登录，如果有登录，我们可以获取用户登录的id，添加到购物车的表中来
		if (request.getSession().getAttribute("user") == null) {
			request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
		} else {
			// 少了为null的判断
			int goodscount = Integer.parseInt(request.getParameter("goodscount"));
			double goodsprice = Double.parseDouble(request.getParameter("goodsprice"));
			int goodsid = Integer.parseInt(request.getParameter("goodsid"));

			String goodsname = request.getParameter("goodsname");
			//获取session用户
			User user = (User) request.getSession().getAttribute("user");
			//数量*单价 总价
			double total = goodscount * (goodsprice);

			ShopCartService scservice = new ShopCartServiceImpl();

			int shopid = Integer.parseInt(request.getParameter("shopid"));

			// shopc购物车对象
			Shopc shopc = new Shopc(goodsid, user.getUserid(), shopid, goodsname, goodscount, total);
			
			//调用业务类的方法addGoods
			boolean flag = scservice.addGoods(shopc);

			AjaxUploadResponse aur = new AjaxUploadResponse();
			aur.setCode(0);

			if (flag) {
				aur.setMsg("操作成功!");
			} else {
				aur.setMsg("操作失败!");
			}

			//
			Gson gson = new Gson();
			String str = gson.toJson(aur);

			out.print(str);
			out.close();

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void doxx(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}
	private void showMyCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		ShopCartService scs = new ShopCartServiceImpl();
		// 获取到用户id
		User user = (User) request.getSession().getAttribute("user");
		if(request.getSession().getAttribute("user")==null) {
			response.sendRedirect("efront/UserLogin.jsp");
			return ;
		}
		
       
		int userid = user.getUserid();
		// 查询全部
		int page = 1;
		int pageSize = 10;
		String keywords = "";
		// 页码和关键字
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		PageData<Shopc> pd = scs.showShopCar(userid, page, pageSize, keywords);

		System.out.println("scs :" + pd);
		List<Shopc> list = pd.getList();
		List<Shopc> shopname = new ArrayList<Shopc>();
		for (Shopc shopc : list) {
			shopname.add(scs.showShopName(shopc));
		}

		for (int i = 0; i < shopname.size(); i++) {
			Shopc s = shopname.get(i);
			System.out.println(s.getShopname());
		}
		if (shopname.size() == 0) {
			request.setAttribute("pd", "测试");
			request.getRequestDispatcher("Cart.jsp").forward(request, response);
		} else {
			request.setAttribute("pd", pd);
			request.setAttribute("shopname", shopname);
			request.setAttribute("keyword", keywords);

			request.getRequestDispatcher("efront/Cart.jsp").forward(request, response);
		}

	}
}
